//
//  YDViewController.m
//  LoveHealth
//
//  Created by 枫哥 on 14-9-25.
//  Copyright (c) 2014年 lanou. All rights reserved.
//

#import "YDViewController.h"
#import "RootTabBarController.h"

@interface YDViewController ()
{
    UIPageControl *_pageControl;
    UIScrollView *_scrollView;
    UILabel *_label;
}

@end

@implementation YDViewController
- (void)dealloc
{
    [_pageControl release];
    [_scrollView release];
    [_label release];
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _scrollView = [[UIScrollView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    _scrollView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_scrollView];
    
    _inButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _inButton.frame = CGRectMake(0, self.view.frame.size.height*7/8, 320, self.view.frame.size.height/8);
    [_inButton setTitle:@"点击跳过动画" forState:UIControlStateNormal];
    [self.view addSubview:_inButton];
    
    UIImageView * view1 = [[UIImageView alloc]initWithFrame:CGRectMake(0,0, 320, self.view.frame.size.height)];
    view1.image = [UIImage imageNamed:@"yindao1.png"];
    [_scrollView addSubview:view1];
    [view1 release];
    UIImageView * view2 = [[UIImageView alloc]initWithFrame:CGRectMake(320,0, 320, self.view.frame.size.height)];
    view2.image = [UIImage imageNamed:@"yiindao2.png"];
    [_scrollView addSubview:view2];
    [view2 release];
    UIImageView * view3 = [[UIImageView alloc]initWithFrame:CGRectMake(640,0, 320, self.view.frame.size.height)];
    view3.image = [UIImage imageNamed:@"yindao3.png"];
    [_scrollView addSubview:view3];
    [view3 release];
    
    view1.contentMode = UIViewContentModeScaleAspectFill;
    view1.clipsToBounds = YES;
    view2.contentMode = UIViewContentModeScaleAspectFill;
    view2.clipsToBounds = YES;
    view3.contentMode = UIViewContentModeScaleAspectFill;
    view3.clipsToBounds = YES;

    
    
    //设置scrollView的滚动区域
    _scrollView.contentSize = CGSizeMake(960, self.view.frame.size.height);
    //设置分页效果
    _scrollView.pagingEnabled = YES;
    //设置是否有回弹效果
    _scrollView.bounces = NO;
    //设置代理
    _scrollView.delegate = self;
    _scrollView.scrollEnabled = YES;
    
    _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(self.view.frame.size.width/3, self.view.frame.size.height-20, 100, 20)];
    _pageControl.backgroundColor = [UIColor clearColor];
    _pageControl.currentPageIndicatorTintColor = UIColorCommon;
    _pageControl.pageIndicatorTintColor = [UIColor whiteColor];
    [self.view addSubview:_pageControl];
    
    _pageControl.numberOfPages = 3;
    [_pageControl addTarget:self action:@selector(handlePageControlChangeAction:) forControlEvents:UIControlEventValueChanged];

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
    [view3 addGestureRecognizer:tap];

    
}

- (void)tap:(UITapGestureRecognizer *)tap
{
    RootTabBarController * rootBC = [[RootTabBarController alloc]init];
    self.view = rootBC.view;
    [rootBC release];
}

- (void)handlePageControlChangeAction:(id)sender
{
    //UIPageControl * pageControl = (UIPageControl *)sender;
    //NSLog(@"current page = %ld",pageControl.currentPage);
    CGFloat offsetX = _scrollView.bounds.size.width * _pageControl.currentPage;
    //根据pageControl的当前页数，设置scrollView的contentOffset显示对应页面
    //    _scrollView.contentOffset = CGPointMake(offsetX, 0);
    //设置动画效果
    [_scrollView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
    //_label.text = [NSString stringWithFormat:@"第%ld页",_pageControl.currentPage+1];
    
    
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //NSLog(@"%s,%d",__FUNCTION__,__LINE__);
    //NSLog(@"Offset = %@",NSStringFromCGPoint(scrollView.contentOffset));
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    //NSLog(@"%s,%d",__FUNCTION__,__LINE__);
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView  willDecelerate:(BOOL)decelerate
{
    //NSLog(@"%s,%d",__FUNCTION__,__LINE__);
}

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    //NSLog(@"%s,%d",__FUNCTION__,__LINE__);
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //NSLog(@"%s,%d",__FUNCTION__,__LINE__);
    //计算scrollView停止滚动时，停留在第几页
    CGPoint offset = scrollView.contentOffset;
    //NSLog(@"offset= %f",offset.x);
    NSInteger pagenumber = offset.x / scrollView.bounds.size.width;
    //NSLog(@"第%ld页",pagenumber+1);
    _pageControl.currentPage = pagenumber;
    _label.text = [NSString stringWithFormat:@"第%ld页",_pageControl.currentPage+1];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
